//
//  LKBindAccountViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/6/30.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKBindAccountViewController.h"
#import "LKEmployerListCell.h"
#import "LKAddEmployeerViewController.h"
@interface LKBindAccountViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSArray *headerArray;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)NSInteger page;
@end

@implementation LKBindAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.isMineIn) {
        self.navigationItem.title = @"员工列表";
    }else{
        self.navigationItem.title = @"关联账号";
    }
    self.page = 0;
    [self getAccountDataWithPage];
    [self createTableview];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(headerReloadData) name:@"addNewEmployerNotice" object:nil];
    // Do any additional setup after loading the view.
}
-(void)createTableview{
    WS(weakSelf)
    LKBaseButton *bottomButton = [LKBaseButton buttonWithFrame:CGRectZero type:UIButtonTypeCustom title:@"新增员工" titleColor:[UIColor whiteColor] layercorne:25 font:[UIFont fontWithName:@"PingFangSC-Medium" size:18] backgroudColor:yellowDefaultColor AndActionBlock:^(UIButton * _Nonnull btn) {
        LKAddEmployeerViewController *vc = [[LKAddEmployeerViewController alloc] init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    [self.view addSubview:bottomButton];
    [bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-30);
        make.height.mas_equalTo(50);
    }];
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKEmployerListCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor = viewDefualtColor;
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(NAVIGATION_BAR_HEIGHT);
        make.bottom.equalTo(bottomButton.mas_top).offset(-10);
    }];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self.tableView reloadData];
}
/// 下拉刷新
-(void)headerReloadData{
    self.page = 0;
    self.tableView.mj_footer.hidden = NO;
    [self getAccountDataWithPage];
}
/// 上拉加载更多
-(void)footerReloadData{
    self.page++;
    [self getAccountDataWithPage];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKEmployerListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKEmployerListCell"];
    cell.model = self.dataArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 62.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] init];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0000000001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LKAccountModel *model = self.dataArray[indexPath.section];
    if (self.valueBlock) {
        self.valueBlock(model.Id,model.user_name);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

/// 获取账号列表数据
-(void)getAccountDataWithPage{
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{@"page":@(self.page)}];
    NSString *sign = [NSString encoingWithDic:param];
    [param setObject:sign forKey:@"sign"];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyBindAccountUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {

    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            if (self.page == 0) {
                [self.dataArray removeAllObjects];
            }
            NSArray *dicArray = responseObject[@"data"][@"data"];
            if (dicArray.count == 0) {
                self.tableView.mj_footer.hidden = YES;
            }
            for (NSDictionary *dic in dicArray) {
                LKAccountModel *model = [LKAccountModel modelWithDictionary:dic];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {

    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
